﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using CommonLibrary.DB;
using CommonLibrary.DataModel.Form;
using System.Data;

namespace ServiceApplication.Dao.Form
{
    public class FormFieldDao
    {
        public int addEntity(FormFieldEntity formfieldEntity)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@form_id", formfieldEntity.form_id));
            parameterList.Add(new SqlParameter("@name", formfieldEntity.name));
            parameterList.Add(new SqlParameter("@fieldtype", formfieldEntity.fieldtype));
            parameterList.Add(new SqlParameter("@formtype", formfieldEntity.formtype));
            parameterList.Add(new SqlParameter("@webtype", formfieldEntity.webtype));
            parameterList.Add(new SqlParameter("@valuescale", formfieldEntity.valuescale));
            parameterList.Add(new SqlParameter("@isquery", formfieldEntity.isquery));
            parameterList.Add(new SqlParameter("@valuedefault", formfieldEntity.valuedefault));
            parameterList.Add(new SqlParameter("@inputfrom", formfieldEntity.inputfrom));
            parameterList.Add(new SqlParameter("@fromparams", formfieldEntity.fromparams));
            parameterList.Add(new SqlParameter("@fromvalue", formfieldEntity.fromvalue));
            parameterList.Add(new SqlParameter("@descn", formfieldEntity.descn));
            parameterList.Add(new SqlParameter("@ifwfjudge", formfieldEntity.ifwfjudge));

            string sql = "insert into wf_formfield (form_id,name,fieldtype,formtype,webtype,valuescale,isquery,valuedefault,inputfrom,fromparams,fromvalue,descn,ifwfjudge) " +
                " values (@form_id,@name,@fieldtype,@formtype,@webtype,@valuescale,@isquery,@valuedefault,@inputfrom,@fromparams,@fromvalue,@descn,@ifwfjudge);select @@identity ";
            string formfield_id = DBExecuteUtil.getUniqueResult(sql, parameterList.ToArray());
            return int.Parse(formfield_id);
        }

        public void DleteEntity(int formfield_id)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@formfield_id", formfield_id));
            string sql = "delete from wf_formfield where formfield_id=@formfield_id ";
            DBExecuteUtil.executeSqlWithPar(sql, parameterList.ToArray());
        }

        public FormFieldEntity QueryEntity(int formfield_id)
        {
            FormFieldEntity formfieldEntity = null;
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@formfield_id", formfield_id));
            string sql = "select formfield_id,form_id,name,fieldtype,formtype,webtype,valuescale,isquery," +
                "valuedefault,inputfrom,fromparams,fromvalue,descn,ifwfjudge from wf_formfield where formfield_id=@formfield_id ";

            var result = DBExecuteUtil.querySqlArray(sql, parameterList.ToArray());
            if (null == result) return null;

            formfieldEntity = new FormFieldEntity();
            formfieldEntity.formfield_id = int.Parse(result[0][0]);
            formfieldEntity.form_id = int.Parse(result[0][1]);
            formfieldEntity.name = result[0][2];
            formfieldEntity.fieldtype = result[0][3];
            formfieldEntity.formtype = result[0][4];
            formfieldEntity.webtype = result[0][5];
            formfieldEntity.valuescale = result[0][6];
            formfieldEntity.isquery = int.Parse(result[0][7]);
            formfieldEntity.valuedefault = result[0][8];
            formfieldEntity.inputfrom = result[0][9];
            formfieldEntity.fromparams = result[0][10];
            formfieldEntity.fromvalue = result[0][11];
            formfieldEntity.descn = result[0][12];
            formfieldEntity.ifwfjudge = result[0][13];
            return formfieldEntity;
        }

        /// <summary>
        /// 结果无返回NULL
        /// </summary>
        /// <param name="form_id"></param>
        /// <param name="type">type="  'text','checkbox'  "</param>
        /// <returns></returns>
        public List<FormFieldEntity> QueryEntitys(int form_id, string type)
        {
            List<FormFieldEntity> formfieldEntityList = new List<FormFieldEntity>();

            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@form_id", form_id));
            string sql = "select formfield_id,form_id,name,fieldtype,formtype,webtype,valuescale,isquery," +
                "valuedefault,inputfrom,fromparams,fromvalue,descn,ifwfjudge from wf_formfield where form_id=@form_id ";
            if (!string.IsNullOrEmpty(type))
            {
                sql += "and webtype in ( " + type + " )";
                //parameterList.Add(new SqlParameter("@formtype", type));
            }

            DataTable dt = DBExecuteUtil.querySqlTable(sql, parameterList.ToArray());

            if (dt.Rows.Count == 0) return null;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                FormFieldEntity formfieldEntity = new FormFieldEntity();
                formfieldEntity.formfield_id = Convert.ToInt32(dt.Rows[i][0]);
                formfieldEntity.form_id = Convert.ToInt32(dt.Rows[i][1]);
                formfieldEntity.name = dt.Rows[i][2].ToString();
                formfieldEntity.fieldtype = dt.Rows[i][3].ToString();
                formfieldEntity.formtype = dt.Rows[i][4].ToString();
                formfieldEntity.webtype = dt.Rows[i][5].ToString();
                formfieldEntity.valuescale = dt.Rows[i][6].ToString();
                formfieldEntity.isquery = Convert.ToInt32(dt.Rows[i][7].ToString());
                formfieldEntity.valuedefault = dt.Rows[i][8].ToString();
                formfieldEntity.inputfrom = dt.Rows[i][9].ToString();
                formfieldEntity.fromparams = dt.Rows[i][10].ToString();
                formfieldEntity.fromvalue = dt.Rows[i][11].ToString();
                formfieldEntity.descn = dt.Rows[i][12].ToString();
                formfieldEntity.ifwfjudge = dt.Rows[i][13].ToString();

                formfieldEntityList.Add(formfieldEntity);
            }
            return formfieldEntityList;
        }



        public void UpdateEntity(FormFieldEntity formfieldEntity)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("@formfield_id", formfieldEntity.formfield_id));
            parameterList.Add(new SqlParameter("@form_id", formfieldEntity.form_id));
            parameterList.Add(new SqlParameter("@name", formfieldEntity.name));
            parameterList.Add(new SqlParameter("@fieldtype", formfieldEntity.fieldtype));
            parameterList.Add(new SqlParameter("@formtype", formfieldEntity.formtype));
            parameterList.Add(new SqlParameter("@webtype", formfieldEntity.webtype));
            parameterList.Add(new SqlParameter("@valuescale", formfieldEntity.valuescale));
            parameterList.Add(new SqlParameter("@isquery", formfieldEntity.isquery));
            parameterList.Add(new SqlParameter("@valuedefault", formfieldEntity.valuedefault));
            parameterList.Add(new SqlParameter("@inputfrom", formfieldEntity.inputfrom));
            parameterList.Add(new SqlParameter("@fromparams", formfieldEntity.fromparams));
            parameterList.Add(new SqlParameter("@fromvalue", formfieldEntity.fromvalue));
            parameterList.Add(new SqlParameter("@descn", formfieldEntity.descn));
            parameterList.Add(new SqlParameter("@ifwfjudge", formfieldEntity.ifwfjudge));
            string sql = "update wf_formfield set form_id=@form_id,name=@name,fieldtype=@fieldtype,formtype=@formtype,webtype=@webtype," +
                " valuescale=@valuescale,isquery=@isquery,valuedefault=@valuedefault,inputfrom=@inputfrom,fromparams=@fromparams,fromvalue=@fromvalue, " +
                " descn=@descn,ifwfjudge=@ifwfjudge where formfield_id=@formfield_id)";

            DBExecuteUtil.executeSqlWithPar(sql, parameterList.ToArray());
        }
    }
}
